/**
 * @TODO
 * 
 * ***/
package Avocado.visual {

	import com.greensock.events.LoaderEvent;
	import com.greensock.loading.ImageLoader;
	
	import flash.display.MovieClip;
	import flash.events.ErrorEvent;
	import flash.events.Event;

	public class SpriteImage extends MovieClip {
		
		public static var IMAGE_ERROR:String = new String('imageError');
		
		private var image:ImageLoader;
		
		public var imgURL:String;
		public var options:Object;

		private var loadTrys:Number = new Number(0);

		public function SpriteImage(_imgURL:String, _options:Object=null){
			super();

			this.imgURL = _imgURL;
			if (_options){
				this.options = _options;
			} else {
				this.options = new Object;
			}
			this.options.smoothing = true;
			this.options.onComplete = loadingComplete;
			
			image = new ImageLoader(imgURL, options);
			image.addEventListener(LoaderEvent.ERROR, error);
			image.load();
		}


		/**
		 * 
		 * ***/
		private function loadingComplete(e:LoaderEvent):void{
			this.addChildAt(image.rawContent, 0);
			this.dispatchEvent(new Event(Event.COMPLETE));
		}


		/**
		 * 
		 * ***/
		private function error(e:LoaderEvent):void{
			this.dispatchEvent(new ErrorEvent(e.type, e.bubbles, e.cancelable, e.text));
		}
		
	}

}